<?php
    include("include/header.inc.php");
    include("include/func.inc.php");
    $_SESSION['page'] = 'event';
    
    include("include/sendMail.inc.php");
    
    //Function to display content if not theme found
    function display_theme_not_found($texts){
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
            //Error : theme not found
        echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
        echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        echo '</p></div>';
    }
    
    echo '<div data-role="content">';

    //Check that the "theme" parameter exist in the url
    if(isset ($_GET['theme'])){
        
        //Array to stock all themes
        $themes_array = array();

        if(strpos($_GET['theme'],'-')==false) //Only one theme
            $themes_array[0] = $_GET['theme'];
        else //More than one theme
            $themes_array = explode("-", $_GET['theme']);
        
        //Username of the current user
        $username = strtoupper($_SESSION['login']);
        
        //SAP connection
        $rfc = connectSAP();
        
        echo '<h2>'. $texts['confirmation'][$_SESSION['lang']] .'</h2>';
        echo '<div class="tableauPage">';
        if (count($themes_array)  > 1)
            echo '<p><b>'. $texts['unregistered_to_themes_conf'][$_SESSION['lang']] .'</b></p>';
        else
            echo '<p><b>'. $texts['unregistered_to_theme_conf'][$_SESSION['lang']] .'</b></p>';
        //Foreach themes
        for($i = 0; $i < sizeof($themes_array); ++$i){
            $id_theme = $themes_array[$i];
            //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --
            //We must know if the function really exists
            $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
            if(!$fce_get_themes){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }

            //Pass import parameters
            saprfc_import ($fce_get_themes,"IV_THEME",$id_theme);

            //Pass table parameters
            saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

            //Call and execute the function
            $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
            if ($rc_get_themes != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION )
                    echo ("Exception raised: ".saprfc_exception($fce_get_themes));
                else 
                    echo ("Call error: ".saprfc_error($fce_get_themes));
                exit;
            }

            //Fetch the data from the internal tables
            $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");
            if ($data_row > 0){//If these theme exist
                $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);

                //-- FUNCTION MODULE: Z_FM_M646_REGISTRATION_CANCEL --
                //We must know if the function really exists
                $fce_registration_cancel = saprfc_function_discover($rfc, "Z_FM_M646_REGISTRATION_CANCEL");
                if(!$fce_registration_cancel){
                    echo "The function module has failed.";
                    echo $rfc;
                    exit;
                }

                //Pass import parameters
                saprfc_import ($fce_registration_cancel,"IV_THEME", $id_theme);
                saprfc_import ($fce_registration_cancel,"IP_USERNAME", $username);

                //Call and execute the function
                $rcRegistration = saprfc_call_and_receive ($fce_registration_cancel);            
                if ($rcRegistration != SAPRFC_OK){
                    if ($rfc == SAPRFC_EXCEPTION )
                        echo ("Exception raised: ".saprfc_exception($fce_registration_cancel));
                    else
                        echo ("Call error: ".saprfc_error($fce_registration_cancel));
                    exit;
                }
                /*else
                    $mail = saprfc_export ($fce_registration_cancel,"EV_MAIL");*/

                //realease the function
                saprfc_function_free($fce_registration_cancel);

                // Message to confirm the registration

                //Variables to display the date in a correct format
                $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
                $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4);
                $timeStart = substr($DATARow['STARTTIME'],0,2).':'.substr($DATARow['STARTTIME'],2,2);
                $timeEnd = substr($DATARow['ENDTIME'],0,2).':'.substr($DATARow['ENDTIME'],2,2);
                $confirmationMessage = '';
                //Confirmation
                $confirmationMessage.= '<b>'.$texts['unregistered_to_the_theme'][$_SESSION['lang']].'</b>';
                $confirmationMessage.= '<h3>'.$texts['theme'][$_SESSION['lang']].' '.$DATARow['TITLE_THEME'].'</h3>';
                $confirmationMessage.= '<div style="margin-left:10px;">';
                    //Dates
                    $confirmationMessage.= $texts['start_date'][$_SESSION['lang']].' '.$dateStart;
                    $confirmationMessage.= ' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeStart.'<br/>';
                    $confirmationMessage.= $texts['end_date'][$_SESSION['lang']].' '.$dateEnd;
                    $confirmationMessage.= ' a '.$timeEnd.'<br/>';
                $confirmationMessage.= '</div>'; 

                if (isset($_SESSION['mail']))
                    sendMail($confirmationMessage,$_SESSION['mail'],'unregister');

                //Title

                echo '<h3>'.$texts['theme'][$_SESSION['lang']].' '.$DATARow['TITLE_THEME'].'</h3>';
                echo '<div style="margin-left:10px;">';
                echo $texts['start_date'][$_SESSION['lang']].' '.$dateStart.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeStart.'<br/>';
                echo $texts['end_date'][$_SESSION['lang']].' '.$dateEnd.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeEnd.'<br/>';
                echo '</div>'; 
            }
            else//These theme does not exist
                display_theme_not_found($texts);
        }
//        echo '</div>';
        //realease the function and close the connection
        saprfc_function_free($fce_get_themes);
        saprfc_close($rfc);
    }
    else//Theme and themes doesn't exist
        display_theme_not_found($texts);
    echo '</br><hr align="center" width="75%" size=1 color="red" />';
    echo '<p align="center"><a href="./index.php" data-role="button" data-inline="true">'.$texts['home'][$_SESSION['lang']].'</a></p>';
    echo '</div></div>'; 
    include('include/footer.inc.php');  // Call footer
?>